/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cbsrs.view;
//
//import com.cashmate.model.*;
//import com.cashmate.controller.*;
import com.cbsrs.controller.UserDAO;
import com.cbsrs.model.Users;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
//import java.util.HashMap;
//import java.util.Map;
//import javax.servlet.http.HttpSession;
//import org.apache.log4j.Logger;
//import org.apache.struts2.ServletActionContext;
//
///**
// *
// * @author CMA Developer
// */
public class AuthenticationAction extends ActionSupport {
//
    private static final Logger log = Logger.getLogger(AuthenticationAction.class);
    private String username, password, role;
    @Override
    public String execute() {
        
        Users authenticatedUser = null;
      log.info("USER ID : " + username + " Password : " + password+ " Role : " + role);
        authenticatedUser = UserDAO.authenticate(username, password,role);
        if (authenticatedUser == null) {
            addActionError("Username and/or Password is incorrect. Please try again!");
            return INPUT;
        }
//        HttpSession session = ServletActionContext.getRequest().getSession(true);
        Map session = ActionContext.getContext().getSession();
        session.put(username, authenticatedUser);

            log.info(authenticatedUser.getFirstName() + " " + authenticatedUser.getLastName() + " of " + authenticatedUser.getCollegeName() + " is now online");
            ActionContext.getContext().setSession(session);
            return SUCCESS;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}
